package org.word.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.word.model.definition.SwaggerModelDefinition;
import org.word.model.definition.SwaggerModelProperty;
import org.word.service.ModelDefinitionService;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 非线程安全
 */
@Service
@Slf4j
public class ModelDefinitionServiceImpl implements ModelDefinitionService {
    LinkedHashMap<String, SwaggerModelDefinition> definitionMap;

    @Override
    public void initDefinitionMap(LinkedHashMap<String, SwaggerModelDefinition> definitionMap) {
        this.definitionMap = definitionMap;
        processAndGetDefinition();
    }


    private void processAndGetDefinition() {
        for (SwaggerModelDefinition swaggerModelDefinition : definitionMap.values()) {
            LinkedHashMap<String, SwaggerModelProperty> properties = swaggerModelDefinition.getProperties();
            for (Map.Entry<String, SwaggerModelProperty> entry : properties.entrySet()) {
                String key = entry.getKey();
                SwaggerModelProperty value = entry.getValue();
                value.setKey(key);
            }
        }

    }

    @Override
    public LinkedHashMap<String, SwaggerModelDefinition> getDefinitionMap() {
        return definitionMap;
    }
}
